package cn.hejiang.basic.service.impl;

import cn.hejiang.basic.mapper.BaseMapper;
import cn.hejiang.basic.query.BaseQuery;
import cn.hejiang.basic.service.IBaseService;
import cn.hejiang.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true, propagation = Propagation.SUPPORTS )
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> mapper;

    // 添加方法
    @Override
    @Transactional  // 默认为readOnly = false, propagation = Propagation.REQUIRED
    public void add(T t) {
        mapper.save(t);
    }

    // 修改方法
    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    // 删除方法
    @Override
    @Transactional
    public void delete(Long id) {
        mapper.remove(id);
    }

    // 查询一条数据
    @Override
    public T queryById(Long id) {
        return mapper.loadById(id);
    }

    // 查询全部数据
    @Override
    public List<T> queryAll() {
        return mapper.loadAll();
    }

    // 高级查询、 分页查询
    @Override
    public PageList<T> queryPage(BaseQuery query) {
        Integer total = mapper.queryCount(query);
        List<T> rows = mapper.queryDate(query);
        return new PageList<>(total, rows);
    }

    // 批量删除
    @Override
    public void patchDelete(List<Long> ids) {
        mapper.patchRemove(ids);
    }

}
